﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLLPC;
using Advantech.Motion;
using Advantech.MotionComponent;

namespace VisionCore
{
    public class AxisHelper : DLLPC.AxisHelper
    {
        public AxisHelper(MotionDevice device, int axisIndex, double pluspermm = 10000)
        {
            motionDevice = device;
            _AxisIndex = axisIndex>=100? axisIndex-100: axisIndex;
            PlusPerMM = pluspermm;

            switch (axisIndex)
            {

                case 1:
                    AxisName = "Entry1_TrackSW_Y";
                    HomeDirection = 1;
                    HomeCrossDistance = 1;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode28;
                    break;
                case 2:
                    AxisName = "Entry2_TrackSW_Y";
                    HomeDirection = 1;
                    HomeCrossDistance = 1;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode28;
                    break;
                case 3:
                    AxisName = "Insp1_CCD_X";
                    HomeDirection = 1;
                    HomeCrossDistance = 0;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode28;
                    break;
                case 4:
                    AxisName = "Insp1_CCD_Y";
                    HomeDirection = 1;
                    HomeCrossDistance = 0;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode28;
                    break;
                case 5:
                    AxisName = "Insp1_CCD_Z";
                    HomeDirection = 0;
                    HomeCrossDistance = 0.1;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode18;
                    break;
                case 6:
                    AxisName = "Insp2_CCD_X";
                    HomeDirection = 1;
                    HomeCrossDistance = 1;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode28;
                    break;
                case 7:
                    AxisName = "Insp2_CCD_Y";
                    HomeDirection = 1;
                    HomeCrossDistance = 1;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode24;
                    break;
                case 8:
                    AxisName = "Insp2_CCD_Z";
                    HomeDirection = 1;
                    HomeCrossDistance = 0.1;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode18;
                    break;
                case 0:
                    AxisName = "Entry1_CV_Y";
                    HomeDirection = 1;
                    HomeCrossDistance = 1;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode28;
                    break;
                case 9:
                    AxisName = "Entry2_CV_Y";
                    HomeDirection = 1;
                    HomeCrossDistance = 1;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode28;
                    break;
                case 10:
                    AxisName = "Insp1_Y";
                    HomeDirection = 1;
                    HomeCrossDistance = 1;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode28;
                    break;
                case 11:
                    AxisName = "Insp2_Y";
                    HomeDirection = 1;
                    HomeCrossDistance = 1;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode28;
                    break;
                case 12:
                    AxisName = "Insp3_Y";
                    HomeDirection = 1;
                    HomeCrossDistance = 0;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode28;
                    break;
                case 13:
                    AxisName = "Insp4_Y";
                    HomeDirection = 1;
                    HomeCrossDistance = 0;
                    HomeMode = (uint)Cia402HomeMode.Cia402HomeMode28;
                    break;
                case 14:
                    AxisName = "CV1_Driver";
                    HomeDirection = 1;
                    //HomeCrossDistance = 2;
                    //HomeMode = (uint)Cia402HomeMode.Cia402HomeMode12;
                    break;
                case 15:
                    AxisName = "CV2_Driver";
                    HomeDirection = 1;
                    //HomeCrossDistance = -1;
                    //HomeMode = (uint)Cia402HomeMode.Cia402HomeMode1;
                    break;
                case 16:
                    AxisName = "Insp1_CV1";
                    HomeDirection = 1;
                    //HomeCrossDistance = -1;
                    //HomeMode = (uint)Cia402HomeMode.Cia402HomeMode2;
                    break;
                case 17:
                    AxisName = "Insp1_CV2";
                    HomeDirection = 1;
                    //HomeCrossDistance = -2;
                    //HomeMode = (uint)Cia402HomeMode.Cia402HomeMode1;
                    break;
                case 18:
                    AxisName = "Insp2_CV1";
                    HomeDirection = 1;
                    //HomeCrossDistance = -1;
                    //HomeMode = (uint)Cia402HomeMode.Cia402HomeMode1;
                    break;
                case 19:
                    AxisName = "Insp2_CV2";
                    HomeDirection = 1;
                    //HomeCrossDistance = -1;
                    //HomeMode = (uint)Cia402HomeMode.Cia402HomeMode1;
                    break;
                case 20:
                    AxisName = "Insp3_CV1";
                    HomeDirection = 1;
                    //HomeCrossDistance = -2;
                    //HomeMode = (uint)Cia402HomeMode.Cia402HomeMode17;
                    break;
                case 21:
                    AxisName = "Insp3_CV2";
                    HomeDirection = 1;
                    //HomeCrossDistance = -2;
                    //HomeMode = (uint)Cia402HomeMode.Cia402HomeMode17;
                    break;
                case 22:
                    AxisName = "Insp4_CV1";
                    HomeDirection = 1;
                    //HomeCrossDistance = -2;
                    //HomeMode = (uint)Cia402HomeMode.Cia402HomeMode17;
                    break;
                case 23:
                    AxisName = "Insp4_CV2";
                    HomeDirection = 1;
                    //HomeCrossDistance = -2;
                    //HomeMode = (uint)Cia402HomeMode.Cia402HomeMode17;
                    break;               
            }
        }

        /// <summary>
        /// 更新比较列表
        /// </summary>
        /// <param name="startPos">起始位置</param>
        /// <param name="Pitch"></param>
        /// <param name="Count">数量</param>
        /// <param name="dir">方向：false 负;true 正</param>
        public void UpdateCmpData(bool dir, double startPos, int count = 1, double pitch = 1)
        {
            if (count < 1 || count > 500)
                return;

            double dbl = 0;
            double pos = startPos;
            double[] dblCmpTableData = new double[512];

            for (int i = 0; i < count - 0; i++)
            {
                dbl = pos;
                dblCmpTableData[i] = dbl * PlusPerMM;
                pos = dir ? pos + pitch : pos - pitch;
            }

            CompareSource = false;
            ComparePulseWidth = 300;
            SetCmpTableData(dblCmpTableData, count, dir);
        }
    }
}
